package com.edu.controller;


import com.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.entity.Course;
import com.edu.entity.Teacher;
import com.edu.entity.vo.CourseInfoVo;
import com.edu.entity.vo.CoursePublishVo;
import com.edu.entity.vo.CourseQuery;
import com.edu.exceptionhandler.MyException;
import com.edu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-17
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class CourseController {

	@Autowired
	private CourseService courseService;

	//课程列表
	//todo 完善条件查询带分页
	@GetMapping
	public R getCourseList(){
		List<Course> list = courseService.list(null);
		return R.ok().data("list",list);
	}

	@PostMapping("/addCourseInfo")
	public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

		String id = courseService.saveCourseInfo(courseInfoVo);
		return R.ok().data("courseId",id);
	}

	//根据课程id查询课程基本信息
	@GetMapping("/getCourseInfo/{courseId}")
	public R getCourseInfo(@PathVariable String courseId){

		CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);

		return R.ok().data("courseInfoVo",courseInfoVo);
	}

	//修改课程
	@PostMapping("/updateCourseInfo")
	public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
		courseService.updateCourseInfo(courseInfoVo);
		return R.ok();
	}

	//根据课程id查询课程确认信息
	@GetMapping("/getPublishCourseId/{id}")
	public R getPublishCourseId(@PathVariable String id){
		CoursePublishVo coursePublishVo = courseService.getPublishCourseId(id);
		return R.ok().data("publishCourse",coursePublishVo);
	}

	//课程最终发布，修改课程状态
	@PostMapping("/publishCourse/{id}")
	public R publishCourse(@PathVariable String id){
		Course course = new Course();
		course.setId(id);
		course.setStatus("Normal");
		courseService.updateById(course);
		return R.ok();
	}

	//删除
	@DeleteMapping("/{courseId}")
	public R deleteCourse(@PathVariable String courseId){

		courseService.removeCourse(courseId);
		return R.ok();
	}


	//分页查询
	@GetMapping("/pageCourse/{current}/{limit}")
	public R pageListCourse(@PathVariable long current, @PathVariable long limit){

		Page<Course> page = new Page<>(current,limit);

		try {
		} catch (Exception e) {
			throw new MyException(404,"执行了自定义异常");
		}

		courseService.page(page,null);
		long total = page.getTotal();
		List<Course> records = page.getRecords();

		return R.ok().data("total",total).data("rows",records);
	}

	//分页条件查询
	@PostMapping("/pageCourseCondition/{current}/{limit}")
	public R pageCourseCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery){

		Page<Course> page = new Page<>(current,limit);
		QueryWrapper<Course> wrapper = new QueryWrapper<>();

		String title = courseQuery.getTitle();
		String status = courseQuery.getStatus();

		if(StringUtils.hasText(title)){
			wrapper.like("title",title);
		}
		if (StringUtils.hasText(status)){
			wrapper.eq("status",status);
		}

		wrapper.orderByDesc("gmt_create");

		courseService.page(page,wrapper);

		long total = page.getTotal();
		List<Course> records = page.getRecords();

		return R.ok().data("total",total).data("rows",records);
	}
}

